<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ artObj.title }}</span>
          <!-- 单图 -->
          <!-- <img v-if="artObj.cover.type === 1" :src="artObj.cover.images" class="thumb"> -->
          <img
          class="thumb"
          v-if="artObj.cover.type === 1"
          v-lazy="artObj.cover.images[0]"
          />
        </div>
        <!-- 多图 -->
        <div class="thumb-box" v-if="artObj.cover.type > 1">
          <img
          v-for="(imgUrl, index) in artObj.cover.images"
          :key="index"
          v-lazy="imgUrl" alt=""
          class="thumb"
          >
          <!-- 使用van-image 是因为它可以设置默认图片 -->
          <!-- <van-image
          v-for="(imgUrl, index) in artObj.cover.images"
          :key="index"
          v-lazy="imgUrl" alt=""
          class="thumb">
          <template v-slot:error>图片跑路了</template>
          </van-image> -->

        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ artObj.aut_name }}</span>
            <span>{{ artObj.comm_count }}</span>
            <span>{{ formatTime(artObj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon v-if="isShow" name="cross" @click.stop="show = true" />
        </div>
      </template>
    </van-cell>
    <div v-if="showReport">

    </div>
    <!-- 反馈面板 -->
    <van-action-sheet
    v-model="show"
    :actions="actions"
    @select="onSelect"
    get-container="body"
    :cancel-text="cancelText"
    @cancel='cancelFn'
    @close="closeFn"
    />
  </div>

</template>

<script>
// 目标1：点击反馈垃圾内容
import { timeAgo } from '@/utils/data.js'
import { firstActions, secondActions } from '@/api/report.js'
import Notify from '@/utils/Notify'

export default {
  props: {
    artObj: Object, // 文章对象
    isShow: {
      type: Boolean,
      default: true // 反馈按钮默认是要显示的，因为首页需要显示
    }
  },
  // 将事件处理方法导入后，不能直接使用，放到方法中中转一下
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消',
      showReport: true
    }
  },
  methods: {
    formatTime: timeAgo,
    onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // console.log(item) item.name就是一级数据
      if (item.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        // console.log(this.artObj)
        this.$emit('dislikeEv', this.artObj.art_id)
        this.show = false
        Notify({ type: 'success', message: '反馈成功' })
      } else { // 二级反馈选项
        this.$emit('reportEv', this.artObj.art_id, item.value)
        // 举报成功后，关闭面板 显示一个举报成功
        this.show = false
        Notify({ type: 'success', message: '举报成功' })
      }
    },
    cancelFn () {
      if (this.cancelText === '返回') {
        this.show = true
        this.actions = firstActions
        this.cancelText = '取消'
      }
    },
    // 修复bug:在二级面板点遮罩层退出，再次点击叉叉，直接显示了二级面板
    closeFn () {
      this.actions = firstActions
      this.cancelText = '取消'
    }

  }

}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
    margin: 0 3px;
    &:first-child{
        margin-left: 0;
    }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
